package com.zhuangfei.hos_timetableview.ability;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.language.OnEnglishDateBuildAdapter;
import com.zhuangfei.hos_timetableview.language.OnEnglishItemBuildAdapter;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnItemBuildAdapter;
import com.zhuangfei.timetable.model.Schedule;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.util.List;

public class ItemStyleAbility extends Ability {

    TimetableView mTimetableView;
    Button moreButton;
    String[] popupMenus;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_custom_item_style);
        StackLayout stInclude = (StackLayout) findComponentById(ResourceTable.Id_st_include);
        stInclude.addComponent(LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_header_simple, stInclude, false));
        moreButton = (Button) findComponentById(ResourceTable.Id_id_more);
        moreButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showPopmenu();
            }
        });
        initTimetableView();
    }

    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mySubjects.add(mySubjects.get(0));
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .showView();
    }

    /**
     * 显示弹出菜单
     */
    public void showPopmenu() {
        if (popupMenus == null) {
            popupMenus = new String[]{"切换英文", "切换中文", "隐藏非本周课程", "显示非本周课程",
                    "边距与弧度设置", "课程项文本", "单个角弧度设置", "非本周背景设置", "修改课程重叠的样式"};
        }
        new XPopup.Builder(getContext())
                .hasShadowBg(false)
                .atView(moreButton)
                .asAttachList(popupMenus, null,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                switch (text) {
                                    case "隐藏非本周课程":
                                        hideNonThisWeek();
                                        break;
                                    case "显示非本周课程":
                                        showNonThisWeek();
                                        break;
                                    case "边距与弧度设置":
                                        setMarginAndCorner();
                                        break;
                                    case "课程项文本":
                                        buildItemText();
                                        break;
                                    case "单个角弧度设置":
                                        setCorner(0, 10, 0, 0);
                                        break;
                                    case "非本周背景设置":
                                        setNonThisWeekBgcolor(Color.YELLOW.getValue());
                                        break;
                                    case "修改课程重叠的样式":
                                        modifyOverlayStyle();
                                        break;
                                    case "切换英文":
                                        changeEnglishLanguage();
                                        break;
                                    case "切换中文":
                                        changeChineseLanguage();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }, 0, 0).show();
    }

    /**
     * 隐藏非本周课程
     * 修改了内容的显示，所以必须更新全部（性能不高）
     * 建议：在初始化时设置该属性
     */
    protected void hideNonThisWeek() {
        mTimetableView.isShowNotCurWeek(false).updateView();
    }

    /**
     * 显示非本周课程
     * 修改了内容的显示，所以必须更新全部（性能不高）
     * 建议：在初始化时设置该属性
     */
    protected void showNonThisWeek() {
        mTimetableView.isShowNotCurWeek(true).updateView();
    }

    /**
     * 设置间距以及弧度
     * 该方法只能同时设置四个角的弧度，设置单个角的弧度可参考下文
     */
    protected void setMarginAndCorner() {
        mTimetableView.cornerAll(0)
                .marTop(0)
                .marLeft(0)
                .updateView();
    }

    /**
     * 设置角度（四个角分别控制）
     *
     * @param leftTop     左上角度
     * @param rightTop    右上角度
     * @param rightBottom 右下角度
     * @param leftBottom  左下角度
     */
    public void setCorner(final int leftTop, final int rightTop, final int rightBottom, final int leftBottom) {
        mTimetableView.callback(new OnItemBuildAdapter() {
            @Override
            public void onItemUpdate(StackLayout layout, Text textView, Text countTextView, Schedule schedule, ShapeElement gd) {
                super.onItemUpdate(layout, textView, countTextView, schedule, gd);
                // 数组8个元素，四个方向依次为左上、右上、右下、左下，
                // 每个方向在数组中占两个元素，值相同
                gd.setCornerRadiiArray(new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom});
            }
        });
        mTimetableView.updateView();
    }

    /**
     * 修改显示的文本
     */
    public void buildItemText() {
        mTimetableView.callback(new OnItemBuildAdapter() {
            @Override
            public String getItemText(Schedule schedule, boolean isThisWeek) {
                if (isThisWeek) {
                    return "[本周]" + schedule.getName();
                }
                return "[非本周]" + schedule.getName();
            }
        }).updateView();
    }

    /**
     * 设置非本周课的背景
     *
     * @param color 非本周课的背景，rgb
     */
    public void setNonThisWeekBgcolor(int color) {
        mTimetableView.colorPool().setUselessColor(color);
        mTimetableView.updateView();
    }

    /**
     * 修改课程重叠的样式，在该接口中，你可以自定义出很多的效果
     */
    protected void modifyOverlayStyle() {
        mTimetableView.callback(new OnItemBuildAdapter() {
            @Override
            public void onItemUpdate(StackLayout layout, Text textView, Text countTextView, Schedule schedule, ShapeElement gd) {
                super.onItemUpdate(layout, textView, countTextView, schedule, gd);
                // 可见说明重叠，取消角标，添加角度
                if (countTextView.getVisibility() == Component.VISIBLE) {
                    countTextView.setVisibility(Component.HIDE);
                    gd.setCornerRadiiArray(new float[]{0, 0, 20, 20, 0, 0, 0, 0});
                }
            }
        });
        mTimetableView.updateView();
    }

    /**
     * 切换为英文
     */
    public void changeEnglishLanguage() {
        mTimetableView.callback(new OnEnglishDateBuildAdapter())
                .callback(new OnEnglishItemBuildAdapter())
                .updateView();
    }

    /**
     * 切换为中文
     */
    public void changeChineseLanguage() {
        mTimetableView.callback((ISchedule.OnDateBuildListener) null)
                .callback((ISchedule.OnItemBuildListener) null)
                .updateView();
    }
}
